/*******************************************************************************
* CHIPSEA F61 DELAY HAL
*******************************************************************************/

#include "hal_aw_86802_delay.h"

#ifdef AW_86802

#include <stdint.h>


static AW_U8 fac_us = 0;
static AW_U16 fac_ms = 0;
#define TMP_SYS_CLOCK 24  //TO CONFIRM

/*******************************************************************************
Brief initialize the systick module to realize delay,1m slice is provided 
@param : none
********************************************************************************/
void Delay_Init(void)
{
	SysTick->CTRL &= 0xfffffff9; //bit2 clean,choose external clock  HCLK/8
	fac_us = TMP_SYS_CLOCK;
	fac_ms = (AW_U16)TMP_SYS_CLOCK * 1000;
}

/********************************************************************************
Brief initialize the systick module to realize delay,1m slice is provided 
Example : Delay_ms(10) -- delay 10 ms
@param  : n if you want delay 10 ms , assigne n as 10  
*********************************************************************************/	
void hal_delay_ms(unsigned int n)
{
	AW_U32 temp = 0;
	SysTick->LOAD = (AW_U32)n * fac_ms; // Loading time
	SysTick->VAL = 0x00; // clean counter
	SysTick->CTRL = 0x01; // Start counting down
	do {
		temp = SysTick->CTRL;
	} while (temp & 0x01 && !(temp & (1 << 16))); // Waiting time arrives
	SysTick->CTRL = 0x00; // close  counter
	SysTick->VAL = 0X00; // clean counter
}

//min 100us
void hal_delay_us(unsigned int n)
{
	AW_U32 temp = 0;
	SysTick->LOAD = n * fac_us; // Loading time
	SysTick->VAL = 0x00; // clean counter
	SysTick->CTRL = 0x01 ; // Start counting down
	do {
		temp=SysTick->CTRL;
	} while (temp & 0x01 && !(temp & (1 << 16))); // Waiting time arrives
	SysTick->CTRL = 0x00; // close  counter
	SysTick->VAL = 0X00; // clean  counter
}

#endif /* End of CS_F61 */
